from django.shortcuts import render
from django.core.paginator import Paginator
from django.db.models import Q
from datetime import datetime
import hashlib

from common.models import Users

def index(request,pIndex):
    '''浏览信息'''
    umod = Users.objects
    mywhere = []
    # 获取、判断并封装关keyword键搜索
    kw = request.GET.get("keyword", None)

    if kw:
        list = umod.filter(Q(username__contains=kw) | Q(name__contains=kw))
        mywhere.append("keyword=" + kw)
    else:
        list = umod.filter()
    # 获取、判断并封装性别sex搜索条件
    sex = request.GET.get('sex', '')
    if sex != '':
        list = list.filter(sex=sex)
        mywhere.append("sex=" + sex)

    print(mywhere)

    # 分页逻辑
    if pIndex == '':
        pIndex = '1'
    pIndex = int(pIndex)
    p = Paginator(list,3)
    list2 = p.page(pIndex)
    plist = p.page_range
    maxpages = p.num_pages  # 最大页数

    # 判断页数是否越界
    if pIndex > maxpages:
        pIndex = maxpages
    if pIndex < 1:
        pIndex = 1

    context = {'usersList': list2, 'plist': plist, 'pIndex': pIndex,'maxpages':maxpages,'mywhere':mywhere}
    return render(request,"myadmin/users/index.html",context)

def add(request):
    '''添加信息'''
    return render(request,"myadmin/users/add.html")

def insert(request):
    '''执行增加'''
    try:
        ob = Users()
        ob.username = request.POST['username']
        ob.name = request.POST['name']
        m = hashlib.md5()
        m.update(bytes(request.POST['password'], encoding="utf8"))
        ob.password = m.hexdigest()
        ob.sex = request.POST['sex']
        ob.address = request.POST['address']
        ob.code = request.POST['code']
        ob.phone = request.POST['phone']
        ob.email = request.POST['email']
        ob.state = 1
        ob.addtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {"info":"添加成功！"}
    except Exception as err:
        print(err)
        context = {"info":"添加失败！"}
    return render(request,"myadmin/info.html",context)

def delete(request,uid):
    '''浏览信息'''
    try:
        ob = Users.objects.get(id=uid)
        ob.delete()
        context={"info":"删除成功！"}
    except Exception as erro:
        print(erro)
        context={"info":"删除失败！"}
    return render(request,"myadmin/info.html",context)

def edit(request,uid):
    '''加载编辑页面信息'''
    try:
        ob = Users.objects.get(id = uid)
        context={"user":ob}
        return render(request,"myadmin/users/edit.html",context)
    except Exception as err:
        print(err)
        context = {"info":"没有找到会员信息！"}
        return render(request,"myadmin/info.html",context)

def update(request,uid):
    '''执行编辑信息'''
    try:
        ob = Users.objects.get(id = uid)
        ob.name = request.POST['name']
        ob.sex = request.POST['sex']
        ob.address = request.POST['address']
        ob.code = request.POST['code']
        ob.phone = request.POST['phone']
        ob.email = request.POST['email']
        ob.state = request.POST['state']
        ob.save()
        context = {"info":"修改成功！"}
    except Exception as err:
        print(err)
        context = {"info":"修改失败！"}
    return render(request,"myadmin/info.html",context)

def reset(request,uid):
    '''重置会员密码,页面跳转'''
    ob = Users.objects.get(id = uid)
    context = {"user":ob}
    return render(request,"myadmin/users/reset.html",context)

def resave(request,uid):
    '''重置会员密码'''
    pass
    try:
        ob = Users.objects.get(id = uid)
        m = hashlib.md5()
        m.update(bytes(request.POST['password'], encoding="utf8"))
        ob.password = m.hexdigest()
        ob.save()
        context = {"info": "密码重置成功！"}
    except Exception as err:
        print(err)
        context = {"info": "密码重置失败，请联系管理员！"}
    return render(request, "myadmin/info.html", context)